/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.activities;

import gr.ebs.gssdroid.R;
import gr.ebs.gssdroid.util.GssPreferences;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class GssSettings extends AbstractGssActivity{
	String tag="GssSettings";
	AlertDialog.Builder adb;// = new AlertDialog.Builder(this);
	GssPreferences prefs;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.prefs = new GssPreferences(this);
        setContentView(R.layout.settings);

        // load screen
        populateScreen();

        this.adb = new AlertDialog.Builder(this);

        final Button savebutton = (Button) findViewById(R.id.settingssave);

        // create anonymous click listener to handle the "save"
        savebutton.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                try {

                    // get the string and do something with it.

                    final EditText email = (EditText) findViewById(R.id.username);
                    if (email.getText().length() == 0) {

                        AlertDialog ad = GssSettings.this.adb.create();
                        ad.setMessage("Please Enter Your Username");
                        ad.show();
                        return;
                    }

                    final EditText serverurl = (EditText) findViewById(R.id.password);
                    if (serverurl.getText().length() == 0) {
                        AlertDialog ad = GssSettings.this.adb.create();
                        ad.setMessage("Please Enter Your Password");
                        ad.show();
                        return;
                    }

                    // save off values
                    GssSettings.this.prefs.setUsername(email.getText().toString());
                    GssSettings.this.prefs.setPassword(serverurl.getText().toString());
                    GssSettings.this.prefs.save();

                    Intent intent = new Intent(getApplication(), GssLogin.class);
        			startActivity(intent);
                    // close out this activity
                    finish();
                } catch (Exception e) {
                    Log.i(GssSettings.this.tag, "Failed to Save Settings [" + e.getMessage() + "]");
                }
            }
        });
    }

    private void populateScreen() {
        try {
            final EditText emailfield = (EditText) findViewById(R.id.username);
            final EditText serverurlfield = (EditText) findViewById(R.id.password);

            emailfield.setText(GssSettings.this.prefs.getUsername());
            serverurlfield.setText(GssSettings.this.prefs.getPassword());
        } catch (Exception e) {

        }
    }
    
    

}
